package com.wavecoler.professor.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.wavecoler.professor.dao.ProblemDAO;
import com.wavecoler.professor.objects.Course;
import com.wavecoler.professor.objects.Problem;
import com.wavecoler.professor.objects.Word;

@SuppressWarnings("serial")
public class RegisterProblemServlet extends HttpServlet {

	private List<String> glossary;

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		glossary = new ArrayList<String>();

		String problemName = request.getParameter("problemname");
		String description = request.getParameter("description");
		String avgDuration = request.getParameter("averageduration");
		String difficultyLevel = request.getParameter("difficultylevel");
		String urlPage = request.getParameter("urlpage");
		String parts[] = request.getParameterValues("glossary");
		String totalobjectsavg  = request.getParameter("totalobjectsavg");
		String course = request.getParameter("course");
		
		if (parts != null)
			for (int i = 0; i < parts.length; i++) {
				glossary.add(parts[i].trim());
			}
		
		HttpSession session = request.getSession();
		ArrayList<Course> coursillos =  (ArrayList<Course>) session.getAttribute("cursos");
		Course cursoproblema = new Course();
		
		for(int i = 0; i < coursillos.size();i++){
			if(course.equals(coursillos.get(i).getCourseName())){
				cursoproblema = coursillos.get(i);
			}
		}
		ArrayList<Word> palabrotas = (ArrayList<Word>)session.getAttribute("palabrotas");
		ArrayList<Word> wordtosave = new ArrayList<Word>();
		
		System.out.println("Tamaño de parts: " + parts.length);
		System.out.println("Tamaño de palabrotas:: " + palabrotas.size());		
		
		for(int i = 0; i < parts.length;i++){
			for(int j = 0;j < palabrotas.size();j++){
				System.out.println("Palabra: " + parts[i]);
				System.out.println("Palabrota: " + palabrotas.get(j).getMainWord());
				if(palabrotas.get(j).getMainWord()	.equals(parts[i])){
					wordtosave.add(palabrotas.get(j));
					System.out.println("Guarde: " +wordtosave.get(i).getMainWord());
					break;
				}
			}
		}
		
		Problem p = new Problem();
		p.setName(problemName);
		p.setDescription(description);
		p.setAvgDuration(Integer.parseInt(avgDuration));
		p.setDifficultyLevel(Integer.parseInt(difficultyLevel));
		p.setUrlPage(urlPage);
		p.setTotalObjectAvg(Integer.parseInt(totalobjectsavg));
		p.setCourse(cursoproblema);
		p.setGlossary(wordtosave);
		
		for(int i = 0; i < p.getGlossary().size();i++){
			System.out.println("Palabras del problema " + p.getGlossary().get(i).getMainWord());
		}
		
		 Client service = Client.create();
		 WebResource webResource = service.resource("http://wavecoler.lag.itesm.mx:8080/WaveColerWebServices/rest/problems");
		 ClientResponse respuesta;
		 respuesta = webResource.path("/insert").type(MediaType.APPLICATION_JSON).put(ClientResponse.class,p);
		 System.out.println("Respuesta de envio... estado: " + respuesta.getStatus());

		 String address = "jsps/Course.jsp";
		    RequestDispatcher dispatcher = request.getRequestDispatcher(address);
		    dispatcher.forward(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
}
